#include "lib.h"

#include "jokcrdmg.h"

int32
JokerCardMgr::getDeckSize()
{
	return 53;
}

bool8
JokerCardMgr::isJoker(uint8 Card)
{
	return (bool8)((Card & 0xF0) == SPECIAL && (Card & 0x0F) == JOKER);
}

uint8
JokerCardMgr::getSuit(uint8 Card)
{
	return (uint8)(Card & 0xF0);
}

uint8
JokerCardMgr::getRank(uint8 Card)
{
	return (uint8)(Card & 0x0F);
}

uint8
JokerCardMgr::makeCard(int32 Index)
{
	if (Index < 52)
		return (uint8)((((Index/13) + 1) << 4) | 
			 ((Index % 13) == 0 ? ACE : ((Index % 13) + 1)));
	else if (Index == 52)
		return (uint8)(SPECIAL | JOKER);
	else
		return (uint8)0xFF;
}

void
JokerCardMgr::encode(uint8 Card, uint8* Buf, int32* Used)
{
	//
	// todo: this implementation is temporary (readable version)
	//

	switch (Card & 0xF0)
	{
		case CLUBS:   Buf[(*Used)++] = 'C'; break;
		case DIAMONDS:Buf[(*Used)++] = 'D'; break;
		case HEARTS:  Buf[(*Used)++] = 'H'; break;
		case SPADES:  Buf[(*Used)++] = 'S'; break;
		case SPECIAL: Buf[(*Used)++] = 'X'; break;
		default:	  Buf[(*Used)++] = '?'; break;
	}
	switch (Card & 0x0F)
	{
		case ACE:	Buf[(*Used)++] = 'A'; break;
		case 10:	Buf[(*Used)++] = 'T'; break;
		case JACK:	Buf[(*Used)++] = 'J'; break;
		case QUEEN:	Buf[(*Used)++] = 'Q'; break;
		case KING:	Buf[(*Used)++] = 'K'; break;
		default:	Buf[(*Used)++] = (uint8)('0' + ((Card) & 0x0F)); break;
	}
}
